package org.example.seckill.validator;

import org.example.seckill.util.ValidatorUtil;
import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author woopsion
 * @date 2025-06-29
 * 自定义校验器 电话校验器
 */
public class IsMobileValidator implements ConstraintValidator<isMobile, String> {
    public boolean require = false;
    @Override
    public void initialize(isMobile constraintAnnotation) {
        require=constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(require){
            return ValidatorUtil.isMobile(Long.parseLong(s));
        }else {
            if (!StringUtils.hasText(s)) {
                return true;
            }else  {
                return ValidatorUtil.isMobile(Long.parseLong(s));
            }
        }
    }
}
